﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Menus;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Menus
{
    public class UpdateMenuCommandHandler : IRequestHandler<UpdateMenuCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Menu> menuRep;

        public UpdateMenuCommandHandler(IBaseRepository<Menu> menuRep)
        {
            this.menuRep = menuRep;
        }
        public Task<APIResult<object>> Handle(UpdateMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var getObj = menuRep.GetAll().Result.FirstOrDefault(x => x.MenuId == request.MenuId);
            getObj.MenuName=request.MenuName;
            getObj.Url = request.Url;
            getObj.PId = request.PId;
            getObj.UpdateName = request.UpdateName;
            getObj.UpdateTime = DateTime.Now;
            var obj = menuRep.Update(getObj).Result;
            if (obj > 0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = obj;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
            }

            return Task.FromResult(result);
        }
    }
}
